package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Bod;
import model.Gebruiker;
import model.Veiling;
import database.ReadDAO;

/**
 * Servlet implementation class VeilingController
 */
public class VeilingController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	PrintWriter out;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VeilingController() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();
		ReadDAO readdao = new ReadDAO();
		HttpSession session = request.getSession();
		Gebruiker gebruiker = (Gebruiker) session.getAttribute("gebruiker");
		String action = request.getParameter("action");
		if(action != null){
			try{
				if(action.equals("myAuctions")){
					ArrayList<Veiling> veilingen = readdao.getVeilingVanGebruiker(gebruiker.getId());
					request.setAttribute("Veilingen", veilingen);
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/myauctions.jsp");
					view.forward(request, response);
					return;
				}
				if(action.equals("newauction")){
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/newauction.jsp");
					view.forward(request, response);
					return;
				}
				if(action.equals("login")){
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/login.jsp");
					view.forward(request, response);
					return;
				}
				if(action.equals("myBids")){
					ArrayList<Bod> biedingen = readdao.getBiedingenVanGebruiker(gebruiker.getId());
					ArrayList<Bod> gewonnen = readdao.getWonAuctions(gebruiker.getId());

					request.setAttribute("gewonnen", gewonnen);
					request.setAttribute("biedingen", biedingen);
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/mybids.jsp");
					view.forward(request, response);
					return;
				}
				if(action.contains("fullauction")){
					int veilingid = Integer.parseInt(request.getParameter("veiling"));
					Veiling veiling = readdao.getVeiling(veilingid);
					if(veiling != null){
						ArrayList<Bod> biedingen = readdao.getBiedingen(veiling.getId());
						request.setAttribute("biedingen", biedingen);
						request.setAttribute("veiling", veiling);
						RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/fullauction.jsp");
						view.forward(request, response);
						return;
					} else {
						RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/errorpages/errorpage.jsp");
						view.forward(request, response);
						return;
					}
				}
				if(action.equals("register")){
					RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/registration.jsp");
					view.forward(request, response);
					return;
				}
			}catch(NumberFormatException nfe){
				RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/errorpages/errorpage.jsp");
				view.forward(request, response);
				return;
			}
		}


		request.setAttribute("Veilingen", readdao.getVeilingen());
		RequestDispatcher view = request.getRequestDispatcher("/WEB-INF/index.jsp");
		view.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}

